function body_load(url, data){
	if(!data){
		var data = {};
	}
	load_html($('#mbody'), url, data);
}
function load_html(o, url, data){
	$(o).fadeOut(200, function(){
		$.ajax({
			context:this,
			url:url,
			data:data,
			dataType: 'html',
			success: function(data){
				var o = $(this);
				o.html(data);
				o.fadeIn(200);
			}
		});
	});
}
/*******************************************************************
 *                   邮件
 ******************************************************************/
function mailer_receivebox(pagesize,pagenum){
	body_load("/mailer/index",{pagesize:pagesize,pagenum:pagenum});
}
function mailer_sendbox(pagesize,pagenum){
	body_load("/mailer/sendbox",{pagesize:pagesize,pagenum:pagenum});
}
function mailer_send(to){
	var mailboxid = getCookie("mailer_mailboxid"),
	winid = 'win-replymail',
 	b = $('#winbackground');
	$('#win').children('[id^=win-]').remove();
	if(!document.getElementById(winid)){
		$('#win').prepend( '<div id="'+winid+'"></div>');
	}
	$.ajax({
		url:'/mailer/send',
		dataType: 'html',
		data:{to:to},
		context: $('#'+winid),
		success: function(data){
			$('#win').show();
			b.show();
			var o = $('#'+winid);			
			o.html(data).addClass('mail-viewbox');
			$('#mailer-send-mailboxid').val(mailboxid);
			o.fadeIn(200);
			var scheight =$(window).height(),
				nht = scheight - 120;
			//框体与背景位置设置
			var left = (scwidth - 800)/2;
			o.css({left:left+'px'}).find('.mail-cont').height(nht-200).width(o.width()-20).end().css('height',nht+'px');
			b.css({'top':'70px','left':(left-10)+'px','width':'820px'}).height(o.height()+20);
		},
		type: 'POST'
	});
}
function mailer_exesend(){
	var mailboxid = getCookie("mailer_mailboxid");
	var to = $('#mailer-send-to').val();
	var subject = $('#mailer-send-subject').val();
	var body = $('#mailer-send-body').val();
	$.ajax({
		url : '/mailer/exesend',
		dataType : 'json',
		timeout: 3000000,
		data : {mailboxid:mailboxid,to:to,subject:subject,body:body, output: 'json'},
		beforeSend: function(){
			startProgress(50,'邮件发送中...');
			doProgress();
		},
		success: function(data){
			$('#win').hide();
			stopProgress();
			if(data.code=='2'){
				message('发送成功！', 'info');
				mailer_sendbox(10,1)
			}else{
				message(data.msg, 'error');
			}
		},
		error:function(xmlhttprequest,textstatus,errordata){
			$('#win').hide();
			var msg = "发送失败:"+errordata;
			message(msg, 'error');
			stopProgress();
		}
	});
	
}
function mailer_viewreceivemail(mailid){
	var mailboxid = getCookie("mailer_mailboxid"),
 	winid = 'win-viewmail',
 	b = $('#winbackground');
	$('#win').children('[id^=win-]').remove();
	if(!document.getElementById(winid)){
		$('#win').prepend('<div id="'+winid+'"></div>');
	}
	$.ajax({
		url:'/mailer/viewreceivemail',
		dataType: 'html',
		data:{mailboxid:mailboxid,mailid:mailid},
		context: $('#'+winid),
		success: function(data){
			$('#win').show();
			b.show();
			var o = $('#'+winid);			
			o.html(data).addClass('mail-viewbox').show();
			var scheight =$(window).height(),
		        nht = scheight - 120;
			//框体与背景位置设置
			var left = (scwidth - 800)/2;
			o.css({left:left+'px'}).find('.mail-cont').height(nht-120).width(800).end().css('height',nht+'px');
			b.css('top','70px').css({left:(left - 10)+'px'}).css('width','820px').height(o.height()+20);
		},
		type: 'POST'
	});
}
function mailer_viewsendmail(mailid){
	var mailboxid = getCookie("mailer_mailboxid"),
 	winid = 'win-viewmail',
 	b = $('#winbackground');
	$('#win').children('[id^=win-]').remove();
	if(!document.getElementById(winid)){
		$('#win').prepend('<div id="'+winid+'"></div>');
	}
	$.ajax({
		url:'/mailer/viewsendmail',
		dataType: 'html',
		data:{mailboxid:mailboxid,mailid:mailid},
		context: $('#'+winid),
		success: function(data){
			$('#win').show();
			b.show();
			var o = $('#'+winid);			
			o.html(data).addClass('mail-viewbox').show();
			var scheight =$(window).height(),
		        nht = scheight - 120;
			//框体与背景位置设置
			var left = (scwidth - 800)/2;
			o.css({left:left+'px'}).find('.mail-cont').height(nht-120).width(800).end().css('height',nht+'px');
			b.css('top','70px').css({left:(left - 10)+'px'}).css('width','820px').height(o.height()+20);
			
		},
		type: 'POST'
	});
}


function mailer_receive(){
	/*
	var mailboxid = getCookie("mailer_mailboxid");
	$.ajax({
		url: '/mailer/receive',
		data: {mailboxid:mailboxid, output:'json'},
		dataType: 'json',
		timeout: 3000000,
		beforeSend: function(){
			startProgress(100,'邮件收取中...');
			doProgress();
		},
		success: function(data){
			stopProgress();
			var msg = data.msg;
			if(data.code != 2){
				message(msg, 'error');
			}else{
				message(msg, 'info');
				mailer_receivebox(10,1);
			}

		},
		error:function(xmlhttprequest,textstatus,errordata){
			var msg = "操作失败:"+errordata;
			message(msg, 'info');
			alert(msg);
			stopProgress();
		}
	});
	*/
	//分段收
	mailer_lastmailcount();		
	//window.location.href='/mailer/index'; 
}
function mailer_lastmailcount(){ 
	var count = 0;
	$.ajax({
		url: '/mailer/lastmailcount',
		dataType: 'json',
		timeout: 30000,
		beforeSend: function(){//进度条
			startProgress(100,'邮件收取中...');
			doProgress();
		},
		success: function(data){
			var count= data;
			if(count==0){
				message("无新邮件", 'info');
				stopProgress();
			}else if(count==-1){
				message("邮箱配置信息有误，链接失败！", 'error');
				stopProgress();
			}else if(count==-2){
				message("链接失败", 'error');
				stopProgress();
			}else if(count>0){
				if(!confirm("共"+count+"封未读邮件，开始收取：")){
					stopProgress();
					return;
				}
				$.ajax({
					url: '/mailer/receive',
					dataType: 'json',
					timeout: 3000000,
					beforeSend: function(){
						//startProgress(100,'邮件收取中...');
						//doProgress();
					},
					success: function(data){
						stopProgress();
						if(data==2){
							message("收取成功！", 'info');
							//mailer_receivebox(10,1);
						}else if(data==-1){
							message("邮箱不存在", 'error');
						}else if(data==-2){
							message("收取失败", 'error');
						}
					},
					error:function(xmlhttprequest,textstatus,errordata){
						var msg = "操作失败:"+errordata;
						message(msg, 'error');
						stopProgress();
						doProgress();
					}
				});
			}else{
				message("收取失败", 'error');
				stopProgress();
			}
		},
		error:function(xmlhttprequest,textstatus,errordata){
			var msg = "操作失败:请检查网络链接或邮箱配置！";
			message(msg, 'error');
			//alert(msg);
			stopProgress();
		}
	});
}
function mailer_deletereceivemail(mailid){
	if(!confirm('确实要删除吗?')){
		return;
	}
	var mailboxid = getCookie("mailer_mailboxid");
	$.ajax({
		url: '/mailer/deletereceivemail',
		data: {mailboxid:mailboxid, mailid:mailid,output:'json'},
		dataType: 'json',
		success: function(data){
			if(typeof data.code != 'undefined'){
				var msg = data.msg;
				if(data.code != 2){
					message(msg, 'error');
				}else{
					message(msg, 'info');
					mailer_receivebox(10,1);
				}
			}else{
				message('系统异常！', 'error');
			}
		}
	});
}
function mailer_deletesendmail(mailid){
	if(!confirm('确实要删除吗?')){
		return;
	}
	var mailboxid = getCookie("mailer_mailboxid");
	$.ajax({
		url: '/mailer/deletesendmail',
		data: {mailboxid:mailboxid, mailid:mailid,output:'json'},
		dataType: 'json',
		success: function(data){
			if(typeof data.code != 'undefined'){
				var msg = data.msg;
				if(data.code != 2){
					message(msg, 'error');
				}else{
					message(msg, 'info');
					mailer_sendbox(10,1);
				}
			}else{
				message('系统异常！', 'error');
			}
		}
	});
}

function mailer_changemailbox(){
	var mailboxid = $('#mailer_curmailboxid').val();
	setCookie("mailer_mailboxid",mailboxid);
	mailer_receivebox(10,1)
}
function mailer_getmailboxlisthtml(){
	var curmailboxid = getCookie("mailer_mailboxid");
	$.ajax({
		url: '/mailer/mailbox',
		data:{output:'json'},
		dataType: 'json',
		success: function(data){
			var i=0;
			var s = "当前邮箱：<select  onchange=\"mailer_changemailbox()\" id=\"mailer_curmailboxid\">"
			for(var d in data){
				if(i==0){
					if(curmailboxid==null||curmailboxid=='')
						setCookie("mailer_mailboxid",data[d].id);
				}
				if(curmailboxid==data[d].id){
					s+= "<option value=\""+data[d].id+"\" selected=selected>"+data[d].display_name+"</option>";
				}else{
					s+= "<option value=\""+data[d].id+"\">"+data[d].display_name+"</option>";
				}
				i++;
			}
			s+="</select>";
			$("#mailer_mailboxlist").html(s);
		}
	});
}
/**************************************************
联系人相关
*************************************************/
 
function mailer_linkman(pagesize,pagenum){
	var mailboxid = getCookie("mailer_mailboxid");
	body_load("/mailer/linkman",{mailboxid:mailboxid,pagesize:pagesize,pagenum:pagenum});
}



/*************************************************************************************
cookie 操作
************************************************************************************/
/************************************************************************
|    函数名称： setCookie                                                |
|    函数功能： 设置cookie函数                                            |
|    入口参数： name：cookie名称；value：cookie值                        |
|    维护记录： Spark(创建）                                            |
|    版权所有： (C) 2006-2007 北京东方常智科技有限公司                    |
|    编写时间： 2007年9月13日 21:00                                        |
*************************************************************************/
function setCookie(name, value) 
{ 
    var argv = setCookie.arguments; 
    var argc = setCookie.arguments.length; 
    var expires = (argc > 2) ? argv[2] : null; 
    if(expires!=null) 
    { 
        var LargeExpDate = new Date (); 
        LargeExpDate.setTime(LargeExpDate.getTime() + (expires*1000*3600*24));         
    } 
    document.cookie = name + "=" + escape (value)+((expires == null) ? "" : ("; expires=" +LargeExpDate.toGMTString())); 
}
/************************************************************************
|    函数名称： getCookie                                                |
|    函数功能： 读取cookie函数                                            |
|    入口参数： Name：cookie名称                                            |
|    维护记录： Spark(创建）                                            |
|    版权所有： (C) 2006-2007 北京东方常智科技有限公司                    |
|    编写时间： 2007年9月13日 21:02                                        |
*************************************************************************/
function getCookie(Name) 
{ 
    var search = Name + "=" 
    if(document.cookie.length > 0) 
    { 
        offset = document.cookie.indexOf(search) 
        if(offset != -1) 
        { 
            offset += search.length 
            end = document.cookie.indexOf(";", offset) 
            if(end == -1) end = document.cookie.length 
            return unescape(document.cookie.substring(offset, end)) 
        } 
        else return "" 
    } 
} 

/************************************************************************
|    函数名称： deleteCookie                                            |
|    函数功能： 删除cookie函数                                            |
|    入口参数： Name：cookie名称                                        |
|    维护记录： Spark(创建）                                        |
|    版权所有： (C) 2006-2007 北京东方常智科技有限公司                |
|    编写时间： 2007年9月15日 18:10                                    |
*************************************************************************/    
function deleteCookie(name) 
{ 
	var expdate = new Date(); 
	expdate.setTime(expdate.getTime() - (86400 * 1000 * 1)); 
    setCookie(name, "", expdate); 
} 